package com.csnf.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtil {
    public static SqlSession getSqlSession()throws IOException {
        // 定义 MyBatis 配置文件的路径
        String path = "mybatis-config.xml";
        // 从类路径中读取 MyBatis 配置文件
        InputStream resourceAsStream= Resources.getResourceAsStream(path);
        // 创建 SqlSessionFactoryBuilder 实例
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        // 使用配置文件构建 SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = builder.build(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        return sqlSession;
    }
}
